<template>
  <!-- <el-dialog  /> -->
  <el-dialog :title="showTitle" :visible="showDialog" @close="close">
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="2-10个字符" size="mini" style="width:80%" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="2-10个字符" size="mini" style="width:80%" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="form.managerId" placeholder="请选择负责人" size="mini" style="width:80%">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>

      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" placeholder="1-100个字符" size="mini" type="textarea" style="width:80%" :rows="4" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button type="primary" size="mini" @click="btnSumbit">确认</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDepartment, getManagerList, updateDepartment, departmentPass, getDepartmentDetail } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false

    },
    departId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      managerList: [],
      form: {
        // 部门名称
        name: '',
        // 部门编码
        code: '',
        // 部门负责人
        managerId: '',
        // 部门介绍
        introduce: '',
        pid: ''

      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称长度为2-10个字符', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            // 查询所有部门数据，名称
            let res = await getDepartment()
            if (this.form.id) {
              res = res.filter(item => item.id !== this.form.id)
            }
            if (res.find(item => item.name === value)) {
              callback(new Error('输入的名称不能重复'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码长度为2-10个字符', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            // 查询所有部门数据，编码
            let res = await getDepartment()
            if (this.form.id) {
              res = res.filter(item => item.id !== this.form.id)
            }
            // console.log(res)
            if (res.find(item => item.code === value)) {
              callback(new Error('输入的编码不能重复'))
            } else {
              callback()
            }
          }, trigger: 'blur' }

        ],
        managerId: [
          { required: true, message: '部门名负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '添加部门'
    }
  },
  created() {
    this.getMList()
  },
  methods: {
    async getDepartmentDetail() {
      const res = await getDepartmentDetail(this.departId)
      console.log(res)
      this.form = res
    },
    async  getMList() {
      const res = await getManagerList()
      console.log(res)
      this.managerList = res
    },
    async btnSumbit() {
      await this.$refs.form.validate()
      // 判断是修改还是新增
      if (this.form.id) {
        // 修改
        await updateDepartment(this.form)
        this.$message.success('修改部门成功')
      } else {
        // 新增
        await departmentPass({ ...this.form, pid: this.departId })
        this.$message.success('添加部门成功')
      }
      this.$emit('updateDepartment')
      this.close()
    },

    close() {
      this.form = {
        // 部门名称
        name: '',
        // 部门编码
        code: '',
        // 部门负责人
        managerId: '',
        // 部门介绍
        introduce: '',
        pid: ''
      }
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
